package com.castle.fortress.admin.member.rest;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.castle.fortress.admin.core.annotation.CastleLog;
import com.castle.fortress.admin.core.constants.GlobalConstants;
import com.castle.fortress.admin.core.sensitivewordfilter.SensitivewordFilter;
import com.castle.fortress.admin.member.dto.MemberCommentDto;
import com.castle.fortress.admin.member.dto.MemberDto;
import com.castle.fortress.admin.member.entity.MemberCommentEntity;
import com.castle.fortress.admin.member.service.MemberCommentService;
import com.castle.fortress.admin.utils.WebUtil;
import com.castle.fortress.common.entity.RespBody;
import com.castle.fortress.common.enums.OperationTypeEnum;
import com.castle.fortress.common.exception.BizException;
import com.castle.fortress.common.respcode.GlobalRespCode;
import com.castle.fortress.common.utils.ConvertUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * 会员评论表 api 控制器
 *
 * @author castle
 * @since 2023-02-20
 */
@Api(tags="会员评论表api管理控制器")
@RestController
@RequestMapping("/api/member/memberComment")
public class ApiMemberCommentController {
    @Autowired
    private MemberCommentService memberCommentService;


	/**
	 * 会员评论表的分页展示 只展示顶级及第一层回复
	 * @param memberCommentDto 会员评论表实体类
	 * @param current 当前页
	 * @param size  每页记录数
	 * @return
	 */
	@CastleLog(operLocation="会员评论表",operType = OperationTypeEnum.QUERY)
	@ApiOperation("会员评论表分页展示")
	@GetMapping("/page")
	public RespBody<IPage<MemberCommentDto>> pageMemberComment(MemberCommentDto memberCommentDto, @RequestParam(required = false) Integer current, @RequestParam(required = false)Integer size){
		MemberDto member = WebUtil.currentMember();
		Integer pageIndex= current==null? GlobalConstants.DEFAULT_PAGE_INDEX:current;
		Integer pageSize= size==null? GlobalConstants.DEFAULT_PAGE_SIZE:size;
		Page<MemberCommentDto> page = new Page(pageIndex, pageSize);
		IPage<MemberCommentDto> pages = memberCommentService.pageMemberCommentExtends(page, memberCommentDto,member);
		return RespBody.data(pages);
	}

    /**
     * 会员评论表的树形展示
     * @param memberCommentDto 会员评论表实体类
     * @return
     */
    @CastleLog(operLocation="会员评论表",operType = OperationTypeEnum.QUERY)
    @ApiOperation("会员评论表树形展示")
    @GetMapping("/tree")
    public RespBody<List<MemberCommentDto>> treeMemberComment(MemberCommentDto memberCommentDto){
		MemberDto member = WebUtil.currentMember();
	    List<MemberCommentDto> list = memberCommentService.treeByParentId(memberCommentDto.getParentId(),member);
//	    //展示时需要展示到顶级
//	    Set<Long> ids=new HashSet<>();
//	    for(MemberCommentDto s:list){
//		    ids.add(s.getId());
//	    }
//	    List<Long> parentId =new ArrayList<>();
//	    List<MemberCommentDto> parentList=new ArrayList<>();
//	    for(MemberCommentDto s:list){
//		    if(s.getParentId()!=null&&!ids.contains(s.getParentId())){
//			    parentId.add(s.getParentId());
//		    }
//	    }
//	    if(!parentId.isEmpty()){
//		    QueryWrapper<MemberCommentEntity> wrapper=new QueryWrapper<>();
//		    wrapper.in("id",parentId);
//		    parentList= ConvertUtil.transformObjList(memberCommentService.list(wrapper),MemberCommentDto.class);
//		    list.addAll(parentList);
//	    }
//	    do{
//		    parentId =new ArrayList<>();
//		    for(MemberCommentDto s:parentList){
//			    if(s.getParentId()!=null){
//				    parentId.add(s.getParentId());
//			    }
//		    }
//		    if(!parentId.isEmpty()){
//			    QueryWrapper<MemberCommentEntity> wrapper=new QueryWrapper<>();
//			    wrapper.in("id",parentId);
//			    parentList= ConvertUtil.transformObjList(memberCommentService.list(wrapper),MemberCommentDto.class);
//			    list.addAll(parentList);
//		    }
//	    }while(!parentId.isEmpty());
	    return RespBody.data(list);
    }

    /**
     * 会员评论表保存
     * @param memberCommentDto 会员评论表实体类
     * @return
     */
    @CastleLog(operLocation="会员评论表",operType = OperationTypeEnum.INSERT)
    @ApiOperation("会员评论表保存")
    @PostMapping("/save")
    public RespBody<MemberCommentEntity> saveMemberComment(@RequestBody MemberCommentDto memberCommentDto){
		MemberDto member = WebUtil.currentMember();
		if(member==null){
			throw new BizException(GlobalRespCode.NO_LOGIN_ERROR);
		}
	    if(memberCommentDto == null ){
		    throw new BizException(GlobalRespCode.PARAM_MISSED);
	    }
		//校验敏感字
		SensitivewordFilter filter = new SensitivewordFilter();
		Set<String> set = filter.getSensitiveWord(memberCommentDto.getContent(), 1);
		if(set.size()>0){
			String forbidWords = set.toString();
			throw new BizException(GlobalRespCode.SENSITIVEWORD_ERROR,forbidWords);
		}
		memberCommentDto.setMemberId(member.getId());
        MemberCommentEntity memberCommentEntity = ConvertUtil.transformObj(memberCommentDto,MemberCommentEntity.class);
	    // 评论主体id和上级id都不为null时 表示为回复的评论
        if(memberCommentEntity.getParentId()!=null && memberCommentEntity.getTargetId() != null){
              MemberCommentEntity parent = memberCommentService.getById(memberCommentEntity.getParentId());
		    if(StrUtil.isEmpty(parent.getParents())){
                memberCommentEntity.setParents(parent.getId()+"");
		    }else{
                memberCommentEntity.setParents(parent.getParents()+","+parent.getId());
		    }
	    }
		memberCommentEntity.setCommentTime(new Date());
	    if(memberCommentService.save(memberCommentEntity)){
		    return RespBody.data(memberCommentEntity);
	    }else{
		    return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
	    }
    }

    /**
     * 会员评论表编辑
     * @param memberCommentDto 会员评论表实体类
     * @return
     */
    @CastleLog(operLocation="会员评论表",operType = OperationTypeEnum.UPDATE)
    @ApiOperation("会员评论表编辑")
    @PostMapping("/edit")
    public RespBody<String> updateMemberComment(@RequestBody MemberCommentDto memberCommentDto){
	    if(memberCommentDto == null || memberCommentDto.getId() == null || memberCommentDto.getId().equals(0L)){
		    throw new BizException(GlobalRespCode.PARAM_MISSED);
	    }
		//校验敏感字
		SensitivewordFilter filter = new SensitivewordFilter();
		Set<String> set = filter.getSensitiveWord(memberCommentDto.getContent(), 1);
		if(set.size()>0){
			String forbidWords = set.toString();
			throw new BizException(GlobalRespCode.SENSITIVEWORD_ERROR,forbidWords);
		}
            MemberCommentEntity memberCommentEntity = ConvertUtil.transformObj(memberCommentDto,MemberCommentEntity.class);
	    if(memberCommentEntity.getParentId()!=null){
              MemberCommentEntity parent = memberCommentService.getById(memberCommentEntity.getParentId());
		    if(StrUtil.isEmpty(parent.getParents())){
                memberCommentEntity.setParents(parent.getId()+"");
		    }else{
                memberCommentEntity.setParents(parent.getParents()+","+parent.getId());
		    }
	    }
	    if(memberCommentService.updateById(memberCommentEntity)){
		    return RespBody.data("保存成功");
	    }else{
		    return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
	    }
    }

    /**
     * 会员评论表删除
     * @param ids 会员评论表id集合
     * @return
     */
    @CastleLog(operLocation="会员评论表",operType = OperationTypeEnum.DELETE)
    @ApiOperation("会员评论表删除")
    @PostMapping("/delete")
    public RespBody<String> deleteMemberComment(@RequestBody List<Long> ids){
	    if(ids == null || ids.size()<1){
		    throw new BizException(GlobalRespCode.PARAM_MISSED);
	    }
	    QueryWrapper<MemberCommentEntity> wrapper=new QueryWrapper<>();
	    wrapper.in("id",ids);
	    for(Long d:ids){
		    wrapper.or().like("parents",d);
	    }
	    if(memberCommentService.remove(wrapper)) {
		    return RespBody.data("删除成功");
	    }else{
		    return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
	    }
    }

	@CastleLog(operLocation="会员评论表",operType = OperationTypeEnum.DELETE)
	@ApiOperation("会员评论表删除")
	@PostMapping("/deleteById")
	public RespBody<String> deleteByIdMemberComment(@RequestParam  Long id ){
		if(id == null){
			throw new BizException(GlobalRespCode.PARAM_MISSED);
		}
		if(memberCommentService.deleteByIdMemberComment(id)) {
			return RespBody.data("删除成功");
		}else{
			return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
		}
	}

    /**
     * 会员评论表详情
     * @param id 会员评论表id
     * @return
     */
    @CastleLog(operLocation="会员评论表",operType = OperationTypeEnum.QUERY)
    @ApiOperation("会员评论表详情")
    @GetMapping("/info")
    public RespBody<MemberCommentDto> infoMemberComment(@RequestParam Long id){
	    if(id == null){
		    throw new BizException(GlobalRespCode.PARAM_MISSED);
	    }
                MemberCommentEntity memberCommentEntity = memberCommentService.getById(id);
                MemberCommentDto memberCommentDto = ConvertUtil.transformObj(memberCommentEntity,MemberCommentDto.class);

	    return RespBody.data(memberCommentDto);
    }


}
